package com.shop.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.admin.service.ICourseAudienceService;
import com.shop.admin.validate.commons.PageValidate;
import com.shop.admin.validate.course.CourseAudienceCreateValidate;
import com.shop.admin.validate.course.CourseAudienceSearchValidate;
import com.shop.common.core.PageResult;
import com.shop.common.entity.course.CourseAudience;
import com.shop.common.enums.UserTypeEnum;
import com.shop.common.mapper.course.CourseAudienceMapper;
import com.shop.common.mapper.course.CourseMapper;
import com.wy.tech.model.TechBasic;
import com.wy.tech.rpc.CollegeRpc;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * 课程实现类
 * @author DragonDean
 */
@Service
public class CourseAudienceServiceImpl implements ICourseAudienceService {
        
    @Resource
    CourseMapper courseMapper;

    @Resource
    CourseAudienceMapper courseAudienceMapper;

    @Resource
    CollegeRpc collegeRpc;

    /**
     * 课程列表
     *
     * @param pageValidate   分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<CourseListedVo>
     * @author DragonDean
     */
    @Override
    public PageResult<CourseAudience> list(PageValidate pageValidate, CourseAudienceSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<CourseAudience> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.eq("course_id", searchValidate.getCourseId());
        queryWrapper.orderByDesc("id");

        courseAudienceMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:userName@user_name:str",
            "=:phone:str",
            "=:userId@user_id:str",
            "=:userType@user_type:int"
        });

        IPage<CourseAudience> iPage = courseAudienceMapper.selectPage(new Page<>(page, limit), queryWrapper);

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    /**
     * 新增课程观众
     *
     * @author DragonDean
     * @param createValidate 参数
     */
    @Override
    public void add(CourseAudienceCreateValidate createValidate) {
        // 查找用户
        TechBasic techBasic = null;
        if(createValidate.getUserType() == UserTypeEnum.TECHNICIAN.getCode()) {
            techBasic = collegeRpc.findTech(createValidate.getPhone()).getData();
        }
        if(createValidate.getUserType() == UserTypeEnum.PARTNER.getCode()){
            techBasic = collegeRpc.findPartner(createValidate.getPhone()).getData();
        }
        if(createValidate.getUserType() == UserTypeEnum.HAN_DI.getCode()){
            techBasic = collegeRpc.findHandiTech(createValidate.getPhone()).getData();
        }
        Assert.notNull(techBasic, "用户不存在!");

        // 查询是否已存在
        CourseAudience modelExist = courseAudienceMapper.selectOne(
                new QueryWrapper<CourseAudience>()
                    .eq("course_id", createValidate.getCourseId())
                    .eq("user_id", techBasic.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));
        Assert.isNull(modelExist, "请勿重复添加!");

        CourseAudience model = new CourseAudience();
        model.setCourseId(createValidate.getCourseId());
        model.setUserId(techBasic.getId());
        model.setUserName(techBasic.getName());
        model.setUserType(createValidate.getUserType());
        model.setPhone(createValidate.getPhone());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        courseAudienceMapper.insert(model);
    }


    /**
     * 删除课程观众
     *
     * @author DragonDean
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        CourseAudience model = courseAudienceMapper.selectOne(
                new QueryWrapper<CourseAudience>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(System.currentTimeMillis() / 1000);
        courseAudienceMapper.updateById(model);
    }

}
